//
//  LiveChatTableViewCell.m
//  YouinRTCDemo
//
//  Created by lprison on 2021/11/10.
//

#import "LiveChatTableViewCell.h"

@implementation LiveChatTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self iniUI];
    }
    return self;
}

-(void)iniUI{
    
    self.contentView.backgroundColor = [UIColor clearColor];
    self.backgroundColor = [UIColor clearColor];
    self.content = [UIView new];
    self.content.backgroundColor = rgba(0, 0, 0, 0.3);
    self.content.layer.cornerRadius = 12;
    self.content.layer.masksToBounds = YES;
    
    self.text = [UILabel new];
    self.text.numberOfLines = 0;
    self.text.font = [UIFont systemFontOfSize:14];
    
    self.count = [UILabel new];
    self.count.numberOfLines = 0;
    self.count.font = [UIFont systemFontOfSize:14];
    self.count.textColor = [UIColor whiteColor];
    
    self.giftImage = [UIImageView new];
    self.giftImage.contentMode = UIViewContentModeScaleAspectFit;
    
    [self.contentView addSubview:self.content];
    [self.content addSubview:self.text];
    [self.content addSubview:self.count];
    [self.content addSubview:self.giftImage];
    [self layoutIfNeeded];
    
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    //判断横竖屏
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    
    [self.content mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(0);
        make.left.mas_equalTo(self.contentView).offset(15);
        make.bottom.mas_equalTo(self.contentView).offset(0);
        if(orientation == UIInterfaceOrientationPortrait){
            make.width.mas_lessThanOrEqualTo(SCREEN_WIDTH-115);
        }else{
            make.width.mas_lessThanOrEqualTo(SCREEN_HEIGHT-115);
        }
    }];
    
    [self.text mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.content).offset(5);
        make.bottom.mas_equalTo(self.content).offset(-5);
        make.left.mas_equalTo(self.content).offset(10);
    }];
    
    [self.giftImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.text.mas_right).offset(0);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
        make.centerY.mas_equalTo(self.text);
    }];
    
    [self.count mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.giftImage.mas_right).offset(0);
        make.right.mas_equalTo(self.content).offset(-10);
        make.centerY.mas_equalTo(self.text);
    }];
    
}

-(void)setMessage:(BaseMessageModel *)message{
    
    if(message.nickName){
        if(message.isGift){
            [self.giftImage mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(self.text.mas_right).offset(0);
                make.width.mas_equalTo(20);
                make.height.mas_equalTo(20);
                make.centerY.mas_equalTo(self.text);
            }];
            [self.giftImage sd_setImageWithURL:[NSURL URLWithString:message.giftUrl] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                
            }];
            NSString *str = [NSString stringWithFormat:@"%@ 赠送了 %@",message.nickName,message.giftName];
            self.text.text = str;
            self.count.text = [NSString stringWithFormat:@"x%d",message.giftCount];
            self.text.textColor = [UIColor whiteColor];
            
        }else{
            self.giftImage.image = nil;
            [self.giftImage mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(self.text.mas_right).offset(0);
                make.width.mas_equalTo(0);
                make.height.mas_equalTo(0);
                make.centerY.mas_equalTo(self.text);
            }];
            self.count.text = @"";
            
            if (![self strIsNULL:message.redId]) {

//                self.text.text = @"管理员发了一个红包";
                NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"管理员发了一个 红包"]];
                NSRange range1 = [[attr string] rangeOfString:@"管理员发了一个"];
                [attr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0,attr.length)];
                
                [attr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,attr.length)];
                
                [attr addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:range1];

                self.text.attributedText = attr;
            }else{
                
                message.text = [message.text stringByReplacingOccurrencesOfString:@"\n" withString:@""];
                
                NSString *displayNickname = message.nickName;
                if (message.nickName.length > 4) {
                    displayNickname = [message.nickName substringToIndex:4];
                    displayNickname = [NSString stringWithFormat:@"%@...",displayNickname];
                }

                NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@：%@", displayNickname, message.text ? message.text : @""]];
                NSRange range1 = [[attr string] rangeOfString:displayNickname];
                [attr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, attr.length)];
                [attr addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, attr.length)];
                [attr addAttribute:NSForegroundColorAttributeName value:message.color range:range1];

                self.text.attributedText = attr;
            }
        }
        
    }else{
        
    }
    
    [self layoutIfNeeded];
   
}


-(BOOL)strIsNULL:(NSString *)str
{
    if ([str isEqualToString:@""]||str.length==0||[[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]length]==0||[str isEqualToString:@"0"]) {
        return YES;
    }else{
        return NO;
    }
}
@end

